package com.filldream.fastboot.web.controller.pc;

import com.filldream.fastboot.base.entity.DatabaseBean;
import com.filldream.fastboot.base.entity.GenTable;
import com.filldream.fastboot.base.request.pc.database.DatabaseSTO;
import com.filldream.fastboot.base.response.pc.database.ColumnInfo;
import com.filldream.fastboot.common.annotation.Log;
import com.filldream.fastboot.common.annotation.RolePermissions;
import com.filldream.fastboot.common.entity.P;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.exception.GlobalException;
import com.filldream.fastboot.core.service.DatabaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author RickSun
 * @version 1.0
 * @data 2021-09-18 16:06
 * @description PcDatabaseController
 */
@AllArgsConstructor
@Api(tags = "PC-数据库管理")
@RestController
@RequestMapping("/pc/database")
public class PcDatabaseController {

    private DatabaseService databaseService;

    @Log(value = "查询数据表列表")
    @ApiOperation(value = "数据表列表",notes = "")
    @GetMapping("/list")
    @RolePermissions(value = "dbManage:list")
    public P<DatabaseBean> list(DatabaseSTO databaseSTO) throws GlobalException {
        return databaseService.list( databaseSTO );
    }

    @Log(value = "查询表字段列表")
    @ApiOperation(value = "表字段列表",notes = "")
    @PostMapping("/column")
    @RolePermissions(value = "dbManage:column")
    public R<List<ColumnInfo>> column(@RequestBody DatabaseSTO databaseSTO) throws GlobalException {
        return databaseService.column( databaseSTO );
    }

    @Log(value = "获取代码配置")
    @ApiOperation(value = "获取代码配置")
    @GetMapping("/tableConfig")
    @RolePermissions(value = "dbManage:tableConfig")
    public R<GenTable> tableConfig(DatabaseSTO databaseSTO){
        return databaseService.tableConfig( databaseSTO );
    }

    @Log(value = "修改代码配置")
    @ApiOperation(value = "修改代码配置")
    @PostMapping("/modifyTableConfig")
    @RolePermissions(value = "dbManage:modifyTableConfig")
    public R<GenTable> modifyTableConfig(@RequestBody  GenTable genTable){
        return databaseService.modifyTableConfig( genTable );
    }

    @Log(value = "同步")
    @ApiOperation(value = "同步代码配置")
    @PostMapping("/refresh")
    @RolePermissions(value = "dbManage:refresh")
    public R<GenTable> refresh(@RequestBody DatabaseSTO databaseSTO){
        return databaseService.refresh( databaseSTO.getTableName() );
    }

    @Log(value = "预览代码")
    @ApiOperation(value = "预览代码")
    @GetMapping("/preview")
    @RolePermissions(value = "dbManage:preview")
    public R<Object> preview(String tableName) throws IOException{
        return databaseService.codeDetail( tableName,1);
    }

    @Log(value = "生成代码")
    @ApiOperation(value = "生成代码")
    @GetMapping("/gen")
    @RolePermissions(value = {"dbManage:gen"})
    public R<Object> gen(String tableName) throws IOException{
        return databaseService.codeDetail( tableName,2);
    }

    @Log(value = "下载代码")
    @ApiOperation(value = "下载代码")
    @GetMapping(value = "/downLoadCode")
    @RolePermissions(value = "dbManage:downLoadCode")
    public void down(String tableName, HttpServletRequest request, HttpServletResponse response)  {
        databaseService.down( tableName,request,response );
    }

}
